﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bzxxxt.admin.service;
using bzxxxt.bll;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// upload_image 的摘要说明
    /// </summary>
    public class upload_image : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                //图片名  
                HttpPostedFile f1 = context.Request.Files["ui_upload_photo_file"];

                String fileExt = System.IO.Path.GetExtension(f1.FileName);                
                if (!(fileExt == ".jpg" || fileExt == ".png"))
                {
                    context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "只支持扩展名为.jpg或.png的图片上传！" }));
                    return;
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss")+ fileExt;
                f1.SaveAs(context.Server.MapPath("~/admin/images/") + fileName);

                //System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
                //int newWidth = 150, newHeight = 150;
                //if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
                //{
                //    newHeight = Convert.ToInt32((decimal)image.Height * newWidth / image.Width);
                //}
                //else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
                //{
                //    newWidth = Convert.ToInt32((decimal)image.Width * newHeight / image.Height);
                //}
                //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
                //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
                //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
                //g.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
                //string fileName = cardno + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
                //bmp.Save(context.Server.MapPath("~/admin/images/") + fileName);
                //bmp.Dispose();
                //image.Dispose();
                context.Response.Write(JsonUtil.ToJson(new { success = true, content = "/admin/images/"+fileName }));
            }
            catch (Exception ex) {
                context.Response.Write(JsonUtil.ToJson(new { success = false, content = "上传出错: "+ex.Message }));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}